﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region

using System;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities.Enum;
using NetBay.Core.Log;
using Rio.Framework.Settings;

#endregion

namespace Rio.Framework.Historical
{
    public static class SettingsTable
    {
        #region Readonly & Static Fields

        /// <summary>
        ///   Table for correspondence Settings/Type
        /// </summary>
        private static readonly Dictionary<GeneralParameterDomain, Dictionary<GeneralParameterName, Type>> m_oDicTypeOfSettings = oDicInitTypeOfSettings();

        #endregion

        #region Class Methods

        /// <summary>
        ///   Translate the setting's value readed from DB to the correct type an value
        /// </summary>
        /// <param name="p_szDomain"> setting's domain readed from DB </param>
        /// <param name="p_szName"> setting's name readed from DB </param>
        /// <param name="p_szReaded"> setting's value readed from DB </param>
        /// <returns> value translated to adequat type in boxed object </returns>
        public static object Translate(string p_szDomain, string p_szName, string p_szReaded)
        {
            object l_oRet = null;
            try
            {
                Type l_oSettingType = null;
                // Find the dictionary of setting's domain

                //GeneralParameterDomain l_eDomain = (GeneralParameterDomain)EnumEx.eGetEnumValueFromDescription(typeof(GeneralParameterDomain), p_szDomain);
                GeneralParameterDomain l_eDomain = Enums.eDescriptionAttributeToDomainSettings(p_szDomain);
                if (m_oDicTypeOfSettings.ContainsKey(l_eDomain))
                {
                    // Retreive the dictionary of names values
                    Dictionary<GeneralParameterName, Type> l_oDicNames = m_oDicTypeOfSettings[l_eDomain];
                    // Find the type of setting's
                    //Settings.GeneralParameterName l_eName = (Settings.GeneralParameterName)EnumEx.eGetEnumValueFromDescription(typeof(Settings.GeneralParameterName), p_szName);
                    GeneralParameterName l_eName = Enums.eDescriptionAttributeToNameSettings(p_szName);
                    if (l_oDicNames.ContainsKey(l_eName))
                    {
                        // Retreive the type of setting
                        l_oSettingType = l_oDicNames[l_eName];
                    }
                    else
                    {
                        LogManager.Instance().LogWarn("(p_szDomain<" + p_szDomain + "> p_szName<" + p_szName + "> p_szReaded<" + p_szReaded + ">) p_szName is not valid!");
                        return null;
                    }
                }
                else
                {
                    LogManager.Instance().LogError("(p_szDomain<" + p_szDomain + "> p_szName<" + p_szName + "> p_szReaded<" + p_szReaded + ">) p_szDomain is not valid!");
                }

                if (l_oSettingType == null)
                    throw new Exception("Error in Translate(p_szDomain<" + p_szDomain + "> p_szName<" + p_szName + "> p_szReaded<" + p_szReaded + ">)");

                // We have a type for this setting, try to convert to a proper type
                if (l_oSettingType.Equals(typeof (bool)))
                {
                    // Try to analyse the string value form our correspondence's table 'm_tListBoolean'
                    l_oRet = FieldRow.Translate(p_szReaded.ToUpper());
                }
                else if (l_oSettingType.Equals(typeof (int)))
                {
                    // Simple converion
                    l_oRet = Convert.ToInt32(p_szReaded);
                }
                else if (l_oSettingType.Equals(typeof (double)))
                {
                    // Simple converion
                    l_oRet = Convert.ToDouble(p_szReaded);
                }
                else if (l_oSettingType.Equals(typeof (long)))
                {
                    // Simple converion
                    l_oRet = Convert.ToInt64(p_szReaded);
                }
                else if (l_oSettingType.Equals(typeof (string)))
                {
                    // No conversion
                    l_oRet = p_szReaded;
                }
                else if (l_oSettingType.Equals(typeof (Tuple<int, int>)))
                {
                    // Split the readed value into an array on string
                    string[] l_aSplit = p_szReaded.Split(new[] {';', ','});
                    if (l_aSplit.Length != 2)
                        LogManager.Instance().LogError("(p_szDomain<" + p_szDomain + "> p_szName<" + p_szName + "> p_szReaded<" + p_szReaded + ">) not match with 'integer value' ;or, 'integer value'");
                    else
                    {
                        // Build a Tuple object to store theses values and convert it in the proper type. here is 'int' and 'int'
                        int l_iOne = Convert.ToInt32(l_aSplit[0]);
                        int l_iTwo = Convert.ToInt32(l_aSplit[1]);
                        l_oRet = new Tuple<int, int>(l_iOne, l_iTwo);
                    }
                }
                else if (l_oSettingType.Equals(typeof (void)))
                {
                    // En attente de Alice
                    LogManager.Instance().LogError("(p_szDomain<" + p_szDomain + "> p_szName<" + p_szName + "> p_szReaded<" + p_szReaded + ">) en attente de Alice");
                }
                else if (l_oSettingType.Equals(typeof (Tuple<string, int>)))
                {
                    // Split the readed value into an array on string
                    string[] l_aSplit = p_szReaded.Split(new[] {';'});
                    if (l_aSplit.Length != 2)
                        LogManager.Instance().LogError("(p_szDomain<" + p_szDomain + "> p_szName<" + p_szName + "> p_szReaded<" + p_szReaded + ">) not match with 'string value' ; 'integer value'");
                    else
                    {
                        // Build a Tuple object to store theses values and convert it in the proper type. here is 'string' and 'int'
                        string l_iOne = l_aSplit[0];
                        int l_iTwo = Convert.ToInt32(l_aSplit[1]);
                        l_oRet = new Tuple<string, int>(l_iOne, l_iTwo);
                    }
                }
                else if (l_oSettingType.Equals(typeof (Tuple<string, string>)))
                {
                    // Split the readed value into an array on string
                    string[] l_aSplit = p_szReaded.Split(new[] {';'});
                    if (l_aSplit.Length != 2)
                        LogManager.Instance().LogError("(p_szDomain<" + p_szDomain + "> p_szName<" + p_szName + "> p_szReaded<" + p_szReaded + ">) not match with 'string value' ; 'integer value'");
                    else
                    {
                        // Build a Tuple object to store theses values and convert it in the proper type. here is 'string' and 'string'
                        string l_iOne = l_aSplit[0];
                        string l_iTwo = l_aSplit[1];
                        l_oRet = new Tuple<string, string>(l_iOne, l_iTwo);
                    }
                }
                else if (l_oSettingType.Equals(typeof (List<string>)))
                {
                    // Split the readed value into an array on string
                    string[] l_aSplit = p_szReaded.Split(new[] {';'});
                    if (l_aSplit.Length == 0)
                        LogManager.Instance().LogError("(p_szDomain<" + p_szDomain + "> p_szName<" + p_szName + "> p_szReaded<" + p_szReaded +
                                                       ">) not match with list of string values separated by ';'");
                    else
                    {
                        l_oRet = l_aSplit;
                    }
                }
                else
                    LogManager.Instance().LogError("(p_szDomain<" + p_szDomain + "> p_szName<" + p_szName + "> p_szReaded<" + p_szReaded + ">) Not handled by RIO");
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("(p_szDomain<" + p_szDomain + "> p_szName<" + p_szName + "> p_szReaded<" + p_szReaded + ">) EXCEPTION -> " + Environment.NewLine + ex);
            }
            return l_oRet;
        }

        /// <summary>
        ///   Initialising all correspondences between a setting and its type
        /// </summary>
        private static Dictionary<GeneralParameterDomain, Dictionary<GeneralParameterName, Type>> oDicInitTypeOfSettings()
        {
            Dictionary<GeneralParameterDomain, Dictionary<GeneralParameterName, Type>> l_oDicRet = new Dictionary<GeneralParameterDomain, Dictionary<GeneralParameterName, Type>>();
            Dictionary<GeneralParameterName, Type> l_oDicName;
            try
            {
                // Add all domains values
                l_oDicRet.Add(GeneralParameterDomain.Authentification, new Dictionary<GeneralParameterName, Type>());
                l_oDicRet.Add(GeneralParameterDomain.Thumbnail, new Dictionary<GeneralParameterName, Type>());
                l_oDicRet.Add(GeneralParameterDomain.Parameter, new Dictionary<GeneralParameterName, Type>());
                l_oDicRet.Add(GeneralParameterDomain.FullText, new Dictionary<GeneralParameterName, Type>());
                //l_oDicRet.Add(GeneralParameterDomain.Login, new Dictionary<GeneralParameterName, Type>());

                // Add all names value for 'GeneralParameterDomain.Authentification'
                l_oDicName = l_oDicRet[GeneralParameterDomain.Authentification];
                l_oDicName.Add(GeneralParameterName.AuthenticationRio, typeof (int));

                // Add all names value for 'GeneralParameterDomain.Thumbnail'
                l_oDicName = l_oDicRet[GeneralParameterDomain.Thumbnail];
                l_oDicName.Add(GeneralParameterName.ThumbnailDefaultDisplayMode, typeof (bool));
                l_oDicName.Add(GeneralParameterName.ThumbnailDirectory, typeof (string));

                // Add all names value for 'GeneralParameterDomain.Parameter'
                l_oDicName = l_oDicRet[GeneralParameterDomain.Parameter];
                l_oDicName.Add(GeneralParameterName.ScanMSI, typeof (string));
                l_oDicName.Add(GeneralParameterName.ScanIdentifier, typeof (string));
                l_oDicName.Add(GeneralParameterName.NumberOfTypesOfRecordsCreated, typeof (int));
                l_oDicName.Add(GeneralParameterName.Storage, typeof (string));
                l_oDicName.Add(GeneralParameterName.Smtp, typeof (string));
                l_oDicName.Add(GeneralParameterName.SmtpLogin, typeof (string));
                l_oDicName.Add(GeneralParameterName.SmtpPassword, typeof (string));
                l_oDicName.Add(GeneralParameterName.AttachmentSize, typeof (long));
                l_oDicName.Add(GeneralParameterName.ChronoPrefix, typeof (string));
                l_oDicName.Add(GeneralParameterName.AdmFlow, typeof (string));
                l_oDicName.Add(GeneralParameterName.EntityDeparture, typeof (int));
                l_oDicName.Add(GeneralParameterName.FolderTypeDeparture, typeof (int));
                l_oDicName.Add(GeneralParameterName.HistoricMaxCountResultsDisplay, typeof (int));
                l_oDicName.Add(GeneralParameterName.KeepCriteria, typeof (bool));
                l_oDicName.Add(GeneralParameterName.ViewerFitToHeight, typeof (bool));
                l_oDicName.Add(GeneralParameterName.ViewerZoomStep, typeof (double));
                l_oDicName.Add(GeneralParameterName.EngineDataBase, typeof (string));

                // Add all names value for 'GeneralParameterDomain.FullText'
                l_oDicName = l_oDicRet[GeneralParameterDomain.FullText];
                l_oDicName.Add(GeneralParameterName.PathIndex, typeof (string));
                l_oDicName.Add(GeneralParameterName.MaxFullTextResult, typeof (int));

                //// Add all names value for 'GeneralParameterDomain.Login'
                //l_oDicName = l_oDicRet[GeneralParameterDomain.Login];
                //l_oDicName.Add(GeneralParameterName.PathRioscanUser, typeof(string));
            }
            catch (Exception ex)
            {
                throw new Exception("dicInitTypeOfSettings() EXCEPTION -> " + Environment.NewLine + ex, ex);
            }
            return l_oDicRet;
        }

        #endregion
    }
}